﻿using MyCrawler.Entity;
using MyCrawler.Utilyties;
using System;
using System.Data.SqlClient;

namespace MyCrawler.Dals
{
    public class ForumTopicDal
    {
        internal string _cn;

        internal ForumTopicDal()
        {
            _cn = UtilityCtr.GetConnectString();
        }

        /// <summary>
        /// Thêm mới 1 danh mục
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        internal int Insert(ForumTopicEntity obj)
        {
            int i = 0;
            using (SqlConnection con = new SqlConnection(_cn))
            {
                try
                {
                    con.Open();

                    string q = "INSERT INTO [ForumTopic]([IdCatagories],[IdTopicGet],[Link],[Subject],[Contents],[IsDeleted],[TotalReply],[Views]";
                    q += ",[Username],[DtPost],[DtReply],[ImgAvatar]) VALUES ('" + obj.IdCatagories + "'";
                    q += ",'" + obj.IdTopicGet + "'";
                    q += ",N'" + obj.Link.Replace("'", "''") + "'";
                    q += ",N'" + obj.Subject.Replace("'","''") + "'";
                    q += ",N'" + obj.Contents.Replace("'", "''") + "'";
                    q += ",'" + obj.IsDeleted + "'";
                    q += ",'" + obj.TotalReply + "'";
                    q += ",'" + obj.View + "'";
                    q += ",N'" + obj.Username.Replace("'", "''") + "'";
                    q += ",'" + obj.DtPost.ToString("yyyy-MM-dd hh:mm:ss") + "'";
                    q += ",'" + obj.DtReply.ToString("yyyy-MM-dd hh:mm:ss") + "'";
                    q += ",N'" + obj.ImgAvatar.Replace("'", "''") + "'";
                    q += ") declare @id int;select @id=ident_current('ForumTopic');select @id";

                    SqlCommand cmd = new SqlCommand(q, con);
                    i = Convert.ToInt32(cmd.ExecuteScalar());
                }
                catch (Exception ex) { }
            }
            return i;
        }

        internal int UdpateContent(int id, string content)
        {
            int i = 0;
            using (SqlConnection con = new SqlConnection(_cn))
            {
                try
                {
                    con.Open();

                    string q = "UPDATE [ForumTopic] SET [Contents] = '" + content + "' WHERE [Id]='" + id + "'";

                    SqlCommand cmd = new SqlCommand(q, con);
                    i = Convert.ToInt32(cmd.ExecuteScalar());
                }
                catch (Exception ex) { throw ex; }
            }
            return i;
        }

        internal int GetIdByUrl(string link)
        {
            int i = 0;
            using (SqlConnection con = new SqlConnection(_cn))
            {
                try
                {
                    con.Open();

                    string q = "Select Top 1 [id] [ForumTopic] WHERE [Link]='" + link + "'";

                    SqlCommand cmd = new SqlCommand(q, con);
                    i = Convert.ToInt32(cmd.ExecuteScalar());
                }
                catch { }
            }
            return i;
        }
    }
}
